#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use LWP::UserAgent;

my $cgi=new CGI;

my %FormValues;

print "Content-Type: text/html\n\n";

my $url = 'https://reserve.opas.jp/cgi-bin/INTMAIN';

$FormValues{"id"}="xfliu";
$FormValues{"pw"}="mypasswd";
$FormValues{"ipw"}="internet_passwd";

#The following one is from the hidden values
$FormValues{"srv"}="MEMBER";
$FormValues{"grp"}="27207";
$FormValues{"ui"}="I1I4";
$FormValues{"sid"}="";
$FormValues{"er"}="0";



print "<html> <head><LINK REL='stylesheet' HREF='http://www.jp-life.org/wiki.css'> </head> <body> <br>
<blockquote> \n";


print "In the code, three vriables <b>have</b> been sent out to the given url. Also please notice that some hidden vaule is also transfered!<br>\n";
print "<li> id=".$FormValues{'id'}."</li>\n";
print "<li> pw=".$FormValues{'pw'}."</li>\n";
print "<li> ipw=".$FormValues{'ipw'}."</li>\n";
print "<li> sent to: ".$url."</li>\n";

print "Response from the give url:
</blockquote><hr> <pre>\n <blockquote>\n";


if(not $url ){
	print "No action link.<br>"; exit();
}


my $agent=$ENV{'HTTP_USER_AGENT'};

my $ua = LWP::UserAgent->new;
$ua->agent($agent);

my $response = $ua->post( $url, \%FormValues );

$response->is_success or  die "Failed to GET '$url': ", $response->status_line;

my $result= $response->as_string;

my $ok=1;

my @re_array=split(/\n/, $result);
my $error;
foreach (@re_array){
	#Here, you can deal with each line of the response, which is just a comman webpage.
	
	print $_;
	
}

print "</pre></blockquote>";

if (0){

	open FH, "<login_2.html";

	foreach (<FH>){
		
		if( /\[\[(.+)\]\]/ ){

			my $tt=$FormValues{$1};
			$tt=~s/\s//g;
			
			my $value=$cgi->param($tt);
			$_=~ s/\[\[(.*)\]\]/$value $tt/g;
		}	
		print $_,"\n";
		
	}

}

# end;
